import getApiData, { getApisData, isCheckProjectIsOpened } from "@/services/cpasRequest";
import { onlineGetApiData, onlineGetApisData } from "@/services/onlineRequest";

export async function getProjectList() {
    try {
        const res = await getApiData('pm.list');
        if (res.code === 0) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}




export async function getIsCheckProjectIsOpened(params: {
    projectBh?: string;
    zcbfid?: string[];
}) {
    try {
        const res = await isCheckProjectIsOpened(params);
        if (res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}


export async function getZcbfList(params: {
    projectBh: string
}) {
    try {
        const res = await getApiData('pm.zcbfList', {
            // usercode: params.usercode,
            projectBh: params.projectBh
        });
        if (res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}

export async function updateZcbfDelName(params: {
    projectCode: string;
}) {
    try {
        const res = await getApiData('pm.updateZcbfDel', params);
        if (res.code === 0) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}



// export async function updateZcbfDel(params: {
//     zcbfID: string;
//     zcbfName: string;
// }) {
//     const res = await getApiData('pm.updateZcbfDel', params);
//     if (res.code === 0) {
//         return Promise.resolve({
//             data: res.data,
//             success: true,
//         });
//     }
//     return Promise.reject(res.data);
// }

export async function getOnlineZcbfList(params: {
    usercode: string | undefined;
    projectID: string
}) {
    // const res = await onlineGetApiData('pm.onlineZcbfList', {
    try {
        const res = await onlineGetApiData('ljjx.pm.zcbfListByProID', {
            usercode: params.usercode,
            projectCode: params.projectID
        });
        if (res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}


// 获取选中的项目、组成部分、人员信息。 ProjectZcbfAndUser
export async function getProjectZcbfAndUser(params: {
    projectCode: string;
}) {
    try {
        const paramsData = {
            apis: [
                {
                    name: "ljjx.pm.projectInfo",
                    // 私用的参数
                    params: {
                    }
                },
                {
                    name: "ljjx.pm.sjxmUserInfo",
                    // 私用的参数
                    params: {
                    }
                },
                {
                    name: "ljjx.pm.zcbfInfo",
                    // 私用的参数
                    params: {
                    }
                }
            ],
            reload: true,
            // 公用的参数
            params: {
                projectCode: params.projectCode
            }
        }
        const res = await onlineGetApisData(paramsData);
        if (res.code === 0 && res.ok) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
};

//插入或更新本地project和zcbf、sjxmuser数据库  ProjectZcbfAndUser 
export async function updateProjectZcbfAndUser(
    projectData: any[],
    updataAllUser: any[],
    zcbfData: any
) {
    try {
        const postData = {
            apis: [
                {
                    name: 'sqlApi.update',
                    params: { name: "ProjectInfo", rows: projectData }
                },
                {
                    name: 'sqlApi.update',
                    params: { name: "sjxmuser", rows: updataAllUser }
                },
                {
                    name: 'sqlApi.update',
                    params: { name: "sjxm_zcbf", rows: zcbfData }
                },

            ],
            reload: true,
            params: {}
        }
        const resApis = await getApisData(postData);
        const resUpdate = resApis.data['sqlApi.update']
        if (resApis.ok && resUpdate.ok) {
            return Promise.resolve({
                data: 'pm.onlineZcbfInfo, pm.onlineSjxmuserList, 更改信息成功！',
                success: true,
            });
        }
        return Promise.resolve({
            data: resApis.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}


//插入本地sjxm_zcbf数据库  ZcbfAndUser
export async function insertZcbfData(
    zcbfData: any[],
) {
    try {
        const postData = {
            apis: [
                {
                    name: 'sqlApi.update',
                    params: { name: "sjxm_zcbf", rows: zcbfData }
                },
            ],
            reload: true,
            params: {}
        }
        const resApis = await getApisData(postData);
        const resUpdate = resApis.data['sqlApi.update']
        if (resApis.ok && resUpdate.ok) {
            return Promise.resolve({
                data: 'pm.onlineZcbfInfo, pm.onlineSjxmuserList, 添加zcbf成功！',
                success: true,
            });
        }
        return Promise.resolve({
            data: resApis.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}

//插入本地sjxm_zcbf数据库  ZcbfAndUser
export async function syncProjectZcbf(params: { zcbfID: string; projectCode: string }) {
    try {
        const res = await getApiData('pm.syncProjectDbname', params);
        if (res.code === 0) {
            return Promise.resolve({
                data: res.data,
                success: true,
            });
        }
        return Promise.resolve({
            data: res.data,
            success: false,
        });
    } catch (error) {
        return Promise.resolve({
            data: 'catch,报错了',
            success: false,
        });
    }
}